package com.kdtech.analyse.news;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.analyse.tool.ParseLogic;
import com.kdtech.analyse.tool.ParseTool;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

public class YouthNewsAnalyse implements AnalyseNews{

	
	public boolean isDetailPage(String url) {
		boolean bRet=false;
		if(url.startsWith("http://tv.")){
			return false;
		}
		String[] regex={
			"http://.*.youth.cn/.*[0-9]{6}/t[0-9]+_[0-9]+[_0-9]*.htm",
			"http://d.youth.cn/[a-z_]+/[0-9]+/t[0-9]+_[0-9]+.html"
		};
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				bRet=true;
				break;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {

		String html=urlMeta.getHtml();
		if (html == null) {
		}
		String title="";
		String content="";;
		String author="";
		String url=urlMeta.getUrl();
		Long date=null;
		NewsMeta meta=new NewsMeta();
		meta.setUrl(url);
		Document doc=Jsoup.parse(html);
		doc.select("a:containsOwn(返回首页)").remove();
		urlMeta=null;
		/*
		 * 解析新闻标题
		 */
		ParseTool tool=new ParseTool(doc);
		tool.addParseLogic(new ParseLogic("div.l_tit"));
		tool.addParseLogic(new ParseLogic("div#articleTitle"));
		tool.addParseLogic(new ParseLogic("div.wenzibiaoti"));
		tool.addParseLogic(new ParseLogic("div.h1"));
		tool.addParseLogic(new ParseLogic("div.hs-h1-top1"));
		tool.addParseLogic(new ParseLogic("h1"));
		tool.addParseLogic(new ParseLogic("td.td08"));
		tool.addParseLogic(new ParseLogic("td.td14[valign=middle]"));
		title=tool.parse();
		if(title!=null && title.contains("相关新闻")) title=StringUtils.substringBefore(title,"相关新闻");
		if(StringUtils.isBlank(title)){
			Elements titleList = doc.select("div.bd_nrky span");
			if(titleList.size() > 0){
				title = titleList.get(0).text();
			}
		}

		/*
		 * 解析时间
		 */
		tool.addParseLogic(new ParseLogic("div.l_time"));
		tool.addParseLogic(new ParseLogic("td.td0311"));
		tool.addParseLogic(new ParseLogic("td.td11"));
		tool.addParseLogic(new ParseLogic("dt.hs-h1-dt02"));
		tool.addParseLogic(new ParseLogic("div.wenzibiaoti2"));
		tool.addParseLogic(new ParseLogic("div.le-con"));
		tool.addParseLogic(new ParseLogic("div.ina_pic_bottom"));
		tool.addParseLogic(new ParseLogic("td.td06"));
		date=tool.parseDate();
		if(date==null){
			date = DateUtils.matchDate(url);
		}


		/*
		 * 解析内容
		 */
		content = HtmlCleaner.getContentHtml(url, doc.select("div#content"));
		if(StringUtils.isBlank(content)){
			content = HtmlCleaner.getContentHtml(url, doc.select("div.article"));
		}
		if(StringUtils.isBlank(content)){
			content = HtmlCleaner.getContentHtml(url, doc.select("div.TRS_Editor"));
		}

		/*
		 * 解析作者
		 */
		tool.addParseLogic(new ParseLogic("div.main_l>div.l_time>p:eq(2)>a"));
		author=tool.parse();
		if(StringUtils.isNotBlank(author)){
			author=author.trim();
		}
		if(StringUtils.isBlank(author)){
			author=JSoupUtils.matchAuthor(doc, "来源：");
		}

		meta.setTitle(title);
		meta.setContent(content);
		meta.setDate(date);
		meta.setAuthor(author);
		return meta;
	}
	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}
	public static void main(String[] args) {
		String url="http://news.youth.cn/gn/201604/t20160425_7911369.htm";
		UrlMeta urlMeta=CrawlHTML.responseToURL(url);
		YouthNewsAnalyse cnhubeiNewsAnalyse=new YouthNewsAnalyse();
		boolean detailPage=cnhubeiNewsAnalyse.isDetailPage(url);
		if(detailPage){
		NewsMeta parserHtml =cnhubeiNewsAnalyse.parserHtml(urlMeta);
		System.out.println(parserHtml);
		cnhubeiNewsAnalyse.Update(parserHtml);
		}else{
			System.out.println("不符合正则");
		}
	}



	
	public boolean isNeedUpdate(){
		return false;
	}


}
